package com.khstudio.videocapture.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.khstudio.videocapture.R;

public class VideoSeekBarView_Kh extends View {
    private static final int RESOURCE_LEFT       = R.drawable.kh_api_seekbar_left;
    private static final int RESOURCE_RIGHT      = R.drawable.kh_api_seekbar_right;
    private static final int RESOURCE_BAR_PASSED = R.drawable.kh_api_seekbar_center_passed;
    private static final int RESOURCE_BAR_SELECT = R.drawable.kh_api_seekbar_center_select;
    private static final int RESOURCE_BAR_NORMAL = R.drawable.kh_api_seekbar_center_normal;

    private Context mContext;

    private Drawable mLeft;
    private Drawable mRight;
    private Drawable mBarFront;
    private Drawable mBarSelected;
    private Drawable mBarRear;

    private int mFrontPos;
    private int mRearPos;

    private int mBarMaxWidth;
    private int mBarStartPosition;

    public VideoSeekBarView_Kh(Context context) {
        this(context, null);
    }

    public VideoSeekBarView_Kh(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VideoSeekBarView_Kh(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mContext = context;

        mLeft = mContext.getResources().getDrawable(RESOURCE_LEFT);
        mLeft.setBounds(0, 0, mLeft.getIntrinsicWidth(), mLeft.getIntrinsicHeight());

        mRight = mContext.getResources().getDrawable(RESOURCE_RIGHT);
        mRight.setBounds(0, 0, mRight.getIntrinsicWidth(), mRight.getIntrinsicHeight());

        mBarFront = mContext.getResources().getDrawable(RESOURCE_BAR_PASSED);

        mBarSelected = mContext.getResources().getDrawable(RESOURCE_BAR_SELECT);

        mBarRear = mContext.getResources().getDrawable(RESOURCE_BAR_NORMAL);

        mFrontPos = 0;
        mRearPos = 0;

        _initAttribute(attrs, defStyle);
    }

    public int getBarMaxWidth() {
        return mBarMaxWidth;
    }

    public int getBarStartPosition() {
        return mBarStartPosition;
    }

    public void setThumbPosition(int position) {
        if(position >= 0 && position <= mBarMaxWidth) {
            mFrontPos = position;
            mRearPos = position;
            invalidate();
        }
    }

    public void setThumbPosition(int front, int rear) {
        if(front >= 0 && front <= mBarMaxWidth && rear >= 0 && rear <= mBarMaxWidth ) {
            if(front < rear) {
                mFrontPos = front;
                mRearPos = rear;
            } else {
                mFrontPos = rear;
                mRearPos = front;
            }
            invalidate();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightSize = mLeft.getIntrinsicHeight();
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int cHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);

        mBarMaxWidth = MeasureSpec.getSize(widthMeasureSpec) - mLeft.getIntrinsicWidth() - mRight.getIntrinsicWidth() - getPaddingLeft() - getPaddingRight();
        mBarStartPosition = getPaddingLeft() + mLeft.getIntrinsicWidth();

        super.onMeasure(widthMeasureSpec, cHeightMeasureSpec);
    }

//    @Override
//    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//        super.onLayout(changed, left, top, right, bottom);
//        KhLog.d("BarDrawView_Kh : onLayout changed = %b l = %d t = %d r = %d b = %d", changed, left, top, right, bottom);
//        mWidth = right - left;
//        mHeight = bottom - top;
//    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int saveCount = canvas.getSaveCount();
        canvas.save();

        if(getPaddingLeft() > 0)
            canvas.translate(getPaddingLeft(), 0);

        mLeft.draw(canvas);
        canvas.translate(mLeft.getIntrinsicWidth(), 0);

        int barPassedWidth = mFrontPos;
        if (barPassedWidth > 0) {
            mBarFront.setBounds(0, 0, barPassedWidth, mBarFront.getIntrinsicHeight());
            mBarFront.draw(canvas);
            canvas.translate(barPassedWidth, 0);
        }

        int barSelectedWidth = mRearPos - mFrontPos;
        if(barSelectedWidth > 0) {
            mBarSelected.setBounds(0, 0, barSelectedWidth, mBarSelected.getIntrinsicHeight());
            mBarSelected.draw(canvas);
            canvas.translate(barSelectedWidth, 0);
        }

        int barNormalWidth = mBarMaxWidth - mRearPos;
        if (barNormalWidth > 0) {
            mBarRear.setBounds(0, 0, barNormalWidth, mBarRear.getIntrinsicHeight());
            mBarRear.draw(canvas);
            canvas.translate(barNormalWidth, 0);
        }

        mRight.draw(canvas);

        canvas.restoreToCount(saveCount);
    }

    private void _initAttribute(AttributeSet attrs, int defStyle) {
        if (attrs == null) {
            return;
        }

        int[] ids = new int[attrs.getAttributeCount()];
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            ids[i] = attrs.getAttributeNameResource(i);
        }

        TypedArray a = mContext.obtainStyledAttributes(attrs, ids, defStyle, 0);

        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            if (attrName == null)
                continue;

        }

        a.recycle();
    }
}
